<template>
  <div>
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增菜单 </a-button>
      </template>
      <template #action="{ record }"> 
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <MenuDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';

  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { GetAllMenu,DeleteMenu } from '@/api/system/menu';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useDrawer } from '@/components/Drawer';
  import  MenuDrawer from './MenuDrawer.vue';
  import { useI18n } from '@/hooks/web/useI18n';
  import { columns, searchFormSchema } from './menu.data';
  import { useLoading } from '@/components/Loading';
  const { notification, createErrorModal } = useMessage();
  const { t } = useI18n();
  export default defineComponent({
    name: 'MenuManagement',
    components: { BasicTable, MenuDrawer, TableAction },
    setup() {
      const [registerDrawer, { openDrawer }] = useDrawer();
       const [openFullLoading, closeFullLoading] = useLoading({
        tip: '加载中...',
      });
      const [registerTable, { reload, expandAll,getForm  }] = useTable({
        title: '菜单列表',
        api: GetAllMenu,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        isTreeTable: true,
        pagination: true,
        striped: false,
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        canResize: false,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });
      function getFormValues() {
        console.log(getForm().getFieldsValue());
      }
      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        // 调取删除菜单接口
        try{
          openFullLoading();
          const res = await DeleteMenu(record.id)
          if(res.code == 200){
            notification.success({
                  message: t('sys.delete.success'),
                  description: t('sys.delete.success'),
                  duration: 3,
                });
                setTimeout(() => {
                  reload();
                }, 500);

          }else{ 
            notification.error({
                  message: t('sys.delete.fail'),
                  description: res.message,
                  duration: 3,
                });
            closeFullLoading();
          }
        }catch(e){
          console.log(e)
        }finally{
          closeFullLoading();
        }
      }

      function handleSuccess() {
        reload();
      }

      function onFetchSuccess(record: Recordable) {
        console.log(record)
        // 演示默认展开所有表项
        // nextTick(expandAll);
      }

      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        onFetchSuccess,
        getFormValues,
      };
    },
  });
</script>
